package com.yfp.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.common.datascope.annotation.DataScope;
import com.yfp.operation.domain.MachineDO;
import com.yfp.operation.domain.OrderListDO;
import com.yfp.operation.domain.dto.*;
import com.yfp.operation.domain.vo.*;
import com.yfp.operation.mapper.OrderListMapper;
import com.yfp.operation.service.OrderListService;
import com.yfp.operation.domain.dto.*;
import com.yfp.operation.domain.vo.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【order_list(订单表)】的数据库操作Service实现
 * @createDate 2024-04-23 14:21:03
 */
@Service
public class OrderListServiceImpl extends ServiceImpl<OrderListMapper, OrderListDO> implements OrderListService {

    @Resource
    private OrderListMapper orderListMapper;

    /**
     * 根据订单id查询订单
     *
     * @param orderIds
     * @return
     */
    @Override
    public List<OrderListDO> getByOrderIds(List<String> orderIds) {
        LambdaQueryWrapper<OrderListDO> wrapper = Wrappers.lambdaQuery(OrderListDO.class).in(OrderListDO::getOrderSn, orderIds);
        return this.list(wrapper);
    }

    @Override
    public List<OrderQueueDTO> orderQueue(String machineId, String phone) {
        List<OrderQueueDTO> orderQueueDTOS = orderListMapper.orderQueue(machineId, phone);
        return orderQueueDTOS;
    }


    @Override
    public void saveCheckCode(OrderDTO orderDTO) {
//        UpdateWrapper<OrderListDO> wrapper = new UpdateWrapper<>().set(OrderListDO::getCheckCode,url)
        LambdaUpdateWrapper<OrderListDO> wrapper = Wrappers.lambdaUpdate(OrderListDO.class).set(OrderListDO::getCheckCode, orderDTO.getTakeFood()).eq(OrderListDO::getOrderSn, orderDTO.getOrderSn());
        this.update(null, wrapper);
    }

    @Override
    public List<String> getOrderSnList(String machineId) {
        return orderListMapper.getOrderSnList(machineId);
    }

    @Override
    public List<String> getUserMobile(String machineId) {
        return orderListMapper.getUserMobile(machineId);
    }

    @Override
    public List<String> getAgain(List<String> mobileList) {
        return orderListMapper.getAgain(mobileList);
    }

    /**
     * 获取所有月销售情况
     *
     * @param machineIds
     * @return
     */
    @Override
    public List<SaleMarketInfoVO> saleMonthInfo(List<String> machineIds) {
        return orderListMapper.saleMonthInfo(machineIds);
    }

    /**
     * 获取本月销售情况
     *
     * @param machineIds
     * @return
     */
    @Override
    public SaleMarketInfoVO getMonthInfo(List<String> machineIds) {
        return orderListMapper.getMonthInfo(machineIds);
    }

    /**
     * 获取本周销售情况
     *
     * @param machineIds
     * @return
     */
    @Override
    public SaleMarketInfoVO getWeekInfo(List<String> machineIds) {
        return orderListMapper.getWeekInfo(machineIds);
    }

    /**
     * 获取当日销售情况
     *
     * @param machineIds
     * @return
     */
    @Override
    public SaleMarketInfoVO getDayInfo(List<String> machineIds) {
        return orderListMapper.getDayInfo(machineIds);
    }

    @Override
    public List<OrderListDO> getByOrderSn(String orderSn) {
        return orderListMapper.getByOrderSn(orderSn);
    }

    /**
     * 根据时间段获取销售情况
     *
     * @param marketInfoDTO
     * @return
     */
    @Override
    public SaleMarketInfoVO marketInfo(MarketInfoDTO marketInfoDTO) {
        if (Objects.isNull(marketInfoDTO) || CollUtil.isEmpty(marketInfoDTO.getMachineIds())) {
            return null;
        }
        return orderListMapper.marketInfo(marketInfoDTO);
    }

    /**
     * 据时间段获取所有支付成功的订单列表
     *
     * @param marketInfoDTO
     * @return
     */
    @Override
    public List<OrderMarketVO> payOkOrderList(MarketInfoDTO marketInfoDTO) {
        if (Objects.isNull(marketInfoDTO) || CollUtil.isEmpty(marketInfoDTO.getMachineIds())) {
            return new ArrayList<>();
        }
        return orderListMapper.payOkOrderList(marketInfoDTO);
    }

    /**
     * 统计时间段支付成功的订单数量
     *
     * @param marketInfoDTO
     * @return
     */
    @Override
    public Integer getCount(MarketInfoDTO marketInfoDTO) {
        if (null == marketInfoDTO || null == marketInfoDTO.getMachineIds()) {
            return null;
        }
        return orderListMapper.getCount(marketInfoDTO);
    }

    @Override
    public List<OrderListDO> findFinish() {
        return orderListMapper.findFinish();
    }

    /**
     * 查询该设备 今日 付过钱没退款的，但是没工单的套餐id
     *
     * @param machineId
     * @return
     */
    @Override
    public List<String> getByMachineIdAndStatus(String machineId) {
        List<String> list = orderListMapper.getByMachineIdAndStatus(machineId);
        return list;
    }

    @Override
    public List<String> getComboIds(String machineId) {
        return orderListMapper.getComboIds(machineId);
    }

    /**
     * 获取当前设备今日退款订单号
     *
     * @param machine
     * @return
     */
    @Override
    public List<OrderDetailVO> getRefundId(MachineDO machine) {
        return orderListMapper.getRefundId(machine);
    }

    @Override
    public List<Sale> countSale(String machineId) {
        return orderListMapper.countSale(machineId);
    }

    @Override
    public List<OrderListDO> listPackStatisticsByPackIds(PackageSalesDataDTO packStatisticsReq, List<Long> packIds) {
        if (CollUtil.isEmpty(packIds)) {
            return List.of();
        }
        List<String> collect = packIds.stream().map(item -> item.toString()).collect(Collectors.toList());
        return orderListMapper.listPackByIds(packStatisticsReq, collect);
    }

    @Override
    public Integer countUserMobileAll(UserLayerDTO userLayerDTO) {
        UserLayerDTO bean = BeanUtil.toBean(userLayerDTO, UserLayerDTO.class);
        bean.setStartDate(null);
        bean.setEndDate(null);
        return orderListMapper.countUserMobile(bean);
    }

    @Override
    public Integer countUserMobileByDate(UserLayerDTO userLayerDTO) {
        UserLayerDTO bean = BeanUtil.toBean(userLayerDTO, UserLayerDTO.class);
        bean.setPayStatus(Arrays.asList(2, 3, 8));
        return orderListMapper.countUserMobile(bean);
    }

    @Override
    public List<OrderListDO> listUserMobile(UserLayerDTO userLayerDTO) {
        return orderListMapper.listUserMobile(userLayerDTO);
    }

    @Override
    public List<UserLayerTotalUserVO> listUserLayerTotalUser(UserLayerTotalUserDTO userLayerTotalUserDTO) {
        userLayerTotalUserDTO.setStartDate(null);
        userLayerTotalUserDTO.setEndDate(null);
        return orderListMapper.listUserLayerTotalUser(userLayerTotalUserDTO);
    }

    @Override
    public List<UserLayerTotalUserVO> listUserLayerOrderUserList(UserLayerTotalUserDTO userLayerDTO) {
        return orderListMapper.listUserLayerOrderUserList(userLayerDTO);
    }

    @Override
    public List<OrderListDO> listNewUserMobile(UserLayerDTO userLayerDTO, List<String> userMobile) {
        return orderListMapper.listNewUserMobile(userLayerDTO, userMobile);
    }

    @Override
    public List<OrderListDO> listFirstUserMobile(UserLayerDTO userLayerDTO, List<String> userMobile) {
        return orderListMapper.listFirstUserMobile(userLayerDTO, userMobile);
    }

    @Override
    public Integer countUserMobileCount2(UserLayerDTO userLayerDTO) {
        return orderListMapper.countUserMobileCount2(userLayerDTO);
    }

    /**
     * 交易数据列表
     * @param dealDataDTO
     * @return
     */
    @Override
    @DataScope(deptAlias = "ar,ro,po,ma")
    public List<DealDataVO> dealDataList(DealDataDTO dealDataDTO) {
        List<DealDataVO> list = orderListMapper.dealDataList(dealDataDTO);
        return list;
    }

    /**
     * 交易数据-总览
     * @param dealDataDTO
     * @return
     */
    @Override
    @DataScope(deptAlias = "ar,ro,po,ma")
    public DealDataVO dealDataTotal(DealDataDTO dealDataDTO) {
        return orderListMapper.dealDataTotal(dealDataDTO);
    }

    /**
     * 修改状态
     * @param payStatus
     * @param orderSn
     */
    @Override
    public void editPayStatusByOrderSn(Integer payStatus, String orderSn) {
        orderListMapper.editPayStatusByOrderSn(payStatus,orderSn);
    }
}